<template>
  <div>
    <h2>商品列表</h2>
    <hr/>
    <ul>
      <li 
        v-for="item,index in goodsList" :key="item.id" 
      >
        {{index+1}}. {{item.id}} {{item.name}} 
        库存: {{item.stock}} 件
        价格 ${{item.price}}
        <button @click="add(item)">加入购物车</button>
      </li>
    </ul>
  </div>
</template>
<script>
import {mapMutations, mapState} from "vuex";
export default {
  computed: {
    ...mapState("goods",["goodsList"])
  },
  methods: {
    ...mapMutations("shopcar", ["addCar"]),
    ...mapMutations("goods",["decreateGoods"]),
    add(item) {
      this.addCar(item);
      this.decreateGoods(item);
    }
  }  
}
</script>